package com.sg.plmadapter;

import java.util.concurrent.ConcurrentHashMap;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.sg.business.model.IPDMServiceProvider;

public class PLMAdapter implements BundleActivator {

	public static final String PLUGIN_ID = "com.sg.plmadapter";
	private static BundleContext context;
	private static ConcurrentHashMap<String, IPDMServiceProvider> serviceCache;
	private static PLMAdapter bundle;
	private PLMService plmService;
	public static final String C_PLM_SYNC = "plmsync";

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext bundleContext) throws Exception {
		PLMAdapter.context = bundleContext;
		bundle = this;

		startPLMService();
	}

	private void startPLMService() {
		plmService = new PLMService();
		plmService.strat();
	}

	private void stopPLMService() {
		plmService.stop();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		stopPLMService();

		PLMAdapter.context = null;
		bundle = null;
	}

	public static IPDMServiceProvider getService(String key) {
		if (serviceCache == null) {
			return null;
		}
		return serviceCache.get(key);
		// return null;
	}

	public static void registeService(String key, IPDMServiceProvider service) {
		if (serviceCache == null) {
			serviceCache = new ConcurrentHashMap<String, IPDMServiceProvider>();
		}
		serviceCache.put(key, service);
	}

	public static PLMService getPLMService() {
		return bundle.plmService;
	}

}
